//
//  DDUpdatePasswordVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/7/31.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDUpdatePasswordVC.h"
#import "DDNetWorkAPIClient.h"
@interface DDUpdatePasswordVC ()

@property (weak, nonatomic) IBOutlet UITextField *orgPwd;
@property (weak, nonatomic) IBOutlet UITextField *nPwd;
@property (weak, nonatomic) IBOutlet UITextField *cPwd;
@end

@implementation DDUpdatePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
}

- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:@"修改密码" parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    __weak DDUpdatePasswordVC *weakSelf = self;
    [self AddRightBtnAction:@"完成" normal:nil selected:nil action:^{
        [weakSelf submit];
    }];
}

-(void)submit
{
    if (_orgPwd.text.length == 0) {
        [self delayHUD:@"请输入原密码"];
        return;
    }
    if (![self validateStringLength:_orgPwd.text])
        return;
    
    if (_nPwd.text.length == 0) {
        [self delayHUD:@"请输入新密码"];
        return;
    }
    if (![self validateStringLength:_nPwd.text])
        return;
    
    if (_cPwd.text.length == 0) {
        [self delayHUD:@"请再输入密码"];
        return;
    }
    if (![self validateStringLength:_cPwd.text])
        return;
    
    if (![_cPwd.text isEqualToString:_nPwd.text]) {
        [self delayHUD:@"两次输入的密码不一致"];
        return;
    }
    
    NSString * orgString = _orgPwd.text;
    NSString * cString = _cPwd.text;

     __weak DDUpdatePasswordVC *weakSelf = self;
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network passwordChange:@"1" oldPassword:orgString password:cString completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
        if (isSuccess) {
            [weakSelf delayHUD:@"修改成功"];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            [weakSelf delayHUD:message];
        }
        
    }];
    
}

-(BOOL)validateStringLength:(NSString *)string
{
    if (string.length < 6 || string.length > 14) {
        [self delayHUD:@"请设置6-14位密码，建议使用数字字母组合"];
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
